<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form onsubmit="return false">
        <input type="text" name="message" value="" />
        <br>
        <input type="button" value="发送websocket请求消息"
        onclick="send(this.form.message.value)">
        <hr color="red">
        <h2>客户端接收到服务端返回的应答消息</h2>
        <textarea name="" id="responseContent" cols="30" rows="10"></textarea>
    </form>
</body>
<script type="text/javascript">
    var socket;
    if(!window.WebSocket){

    }
    if (window.WebSocket){
        socket = new WebSocket("ws://localhost:8888/aaa");
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseContent');
            ta.value += event.data + "\r\n";
        };

        socket.onopen = function (event) {
            var ta = document.getElementById('responseContent');
            ta.value = "您当前的浏览器支持websocket，继续后续操作\r\n";
        };

        socket.onclose = function (event) {
            var ta = document.getElementById('responseContent');
            ta.value = "";
            ta.value = "websocket链接已经关闭\r\n";
        }
    }else {
        alert("不支持websocket")
    }

    function send(msg) {
        if (!window.WebSocket){
            return;
        }
        if (socket.readyState == WebSocket.OPEN){
            socket.send(msg);
        }else {
            alert("websocket链接没有建立成功!!")
        }
    }
</script>
</html>